home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / IWF12.ZIP / KERNEL / GEO.C next >
C/C++ Source or Header  |  1994-02-19  |  9KB  |  307 lines

  1. /*
  2. -------------------------------------------------------------------------------
  3. GEOM.C  (Geometric Image Operations)
  4.  
  5. CREATOR: Craig Muller P.Eng. 1991,1992,1993
  6.             cmuller@ccu.umanitoba.ca
  7.             Computer Vision Laboratory
  8.             University of Manitoba
  9.             Winnipeg, Manitoba. R3T 2N2
  10.             CANADA.
  11.  
  12. DESCRIPTION:
  13.  
  14. These procedures provide a general purpose interface to the image
  15. data and only have compatibility with the Windows environment.
  16.  
  17. PROCEDURES:
  18. ReduceImage()    - Creates a new image which is a reduction of the original.
  19. ScaleImage()     - Creates a new image which scales the original into it.
  20. MirrorImage()    - Creates a new image which is a mirror of the original
  21. FlipImage()      - Creates a new image which is a flip of the original
  22. RotateImage()    - Creates a new image which is a rotation of the original
  23. -------------------------------------------------------------------------------
  24. */
  25. #include <windows.h>
  26.  
  27. #pragma hdrstop
  28.  
  29. #include <stdio.h>
  30. #include <dos.h>
  31. #include <math.h>
  32. #include <mem.h>
  33. #include <alloc.h>
  34. #include <string.h>
  35.  
  36. #include "kernel.h"
  37.  
  38. //---------------------------------------------------------------------------
  39. // Procedure prototypes.
  40. //---------------------------------------------------------------------------
  41. IMAGE *MirrorImage(IMAGE *s_image);
  42. IMAGE *FlipImage  (IMAGE *s_image);
  43. IMAGE *ScaleImage (IMAGE *s_image,int w,int h);
  44. IMAGE *RotateImage(IMAGE *s_image,int dir);
  45. IMAGE *ReduceImage(IMAGE *s_image,int reduction);
  46.  
  47. void ScaleImageRow(IMAGE *s_image,IMAGE *d_image,int y);
  48.  
  49.  
  50. /*
  51. ---------------------------------------------------------------------------
  52. MirrorImage()
  53. ~~~~~~~~~~~~
  54.  
  55. Creates a new image with the same dimensions and then mirrors the original
  56. image into that space. The orginal image is not destroyed.
  57. ---------------------------------------------------------------------------
  58. */
  59. IMAGE *MirrorImage(IMAGE *s_image)
  60.     {
  61.     int col,w,h;
  62.     BYTE  *buf;
  63.     IMAGE *image;
  64.  
  65.     // Set the dimensions of the processing rectangle
  66.     w = s_image->hres;
  67.     h = s_image->vres;
  68.  
  69.     // Create a new image to hold the output data.
  70.     image = DuplicateImage(s_image);
  71.  
  72.     // Mirror the image data using a temporary storage buffer.
  73.     buf = (BYTE *)malloc(h);            // Allocate scan buffer.
  74.     for (col=0; col<w; ++col)           // For each row in the image
  75.         {                                          // Write the row to the col
  76.         GetImageCol(s_image,col   ,0,buf,h);
  77.         PutImageCol(image,w-1-col,0,buf,h);
  78.         }
  79.     free(buf);                                    // Free the scan buffer.
  80.  
  81.     // Return the new image.
  82.     return(image);
  83.     }
  84.  
  85. /*
  86. --------------------------------------------------------------------------
  87. FlipImage()
  88. ~~~~~~~~~~~~
  89.  
  90. Creates a new image with the same dimensions and then flips the original
  91. image into that space. The orginal image is not destroyed.
  92. --------------------------------------------------------------------------
  93. */
  94. IMAGE *FlipImage(IMAGE *s_image)
  95.     {
  96.     int row,w,h;
  97.     BYTE  *buf;
  98.     IMAGE *d_image;
  99.  
  100.     // Set the dimensions of the processing rectangle
  101.     w = s_image->hres;
  102.     h = s_image->vres;
  103.  
  104.     // Create a new image to hold the output data.
  105.     d_image = DuplicateImage(s_image);
  106.  
  107.     // Flip the image data using a temporary storage buffer.
  108.     buf = (BYTE *)malloc(d_image->hres);            // Allocate scan buffer.
  109.     for (row=0; row<h; ++row)                     // For each row in the image
  110.         {                                          // Write the row to the col
  111.         GetImageRow(s_image,0  ,row,buf,w);
  112.         PutImageRow(d_image,0,h-1-row,buf,w);
  113.         }
  114.     free(buf);                                    // Free the scan buffer.
  115.  
  116.     // Return the new image.
  117.     return(d_image);
  118.     }
  119.  
  120.  
  121. /*
  122. --------------------------------------------------------------------------
  123. ScaleImage()
  124. ~~~~~~~~~~~~
  125.  
  126. Creates a new image with the dimensions given and then scales the original
  127. image into that space. The orginal image is not destroyed.
  128. --------------------------------------------------------------------------
  129. */
  130. IMAGE *ScaleImage(IMAGE *s_image,int w,int h)
  131.     {
  132.     int  x,y,x0,y0;
  133.     BYTE  *irow;
  134.     BYTE  *orow;
  135.     IMAGE *d_image;
  136.  
  137.     // Create new image buffer
  138.     d_image = CreateImage(w,h);
  139.  
  140.     // Copy image parameters
  141.     d_image->color = s_image->color;
  142.     strcpy(d_image->fspec,s_image->fspec);
  143.  
  144.     irow = (BYTE *)malloc(s_image->hres);          // Allocate input buffer.
  145.     orow = (BYTE *)malloc( d_image->hres);          // Allocate output buffer.
  146.  
  147.     // Transform the data to the new image
  148.     for (y=0; y<h; ++y)
  149.         {
  150.         y0 = (long)(y) * (long)s_image->vres/(long)h;
  151.         GetImageRow(s_image,0,y0,irow,s_image->hres);// Read pixel row from image
  152.         for (x=0; x<w; ++x)
  153.             {
  154.             x0 = (long)(x) * (long)s_image->hres/(long)w;
  155.             orow[x] = irow[x0];
  156.             }
  157.         PutImageRow(d_image,0,y,orow,d_image->hres);   // Write output row
  158.         }
  159.  
  160.     free(irow);                                   // Free input buffer.
  161.     free(orow);                                   // Free output buffer.
  162.  
  163.     // Copy palette data to the new image
  164.     CopyImagePal(s_image,d_image);
  165.  
  166.     return(d_image);
  167.     }
  168.  
  169.  
  170.  
  171. /*
  172. --------------------------------------------------------------------------
  173. RotateImage()
  174. ~~~~~~~~~~~~
  175.  
  176. Creates a new image with the dimensions reversed and then rotates the original
  177. image left or right to fit into that space. The orginal image is not destroyed.
  178. --------------------------------------------------------------------------
  179. */
  180. IMAGE *RotateImage(IMAGE *s_image,int dir)
  181.     {
  182.     int  row,x,y,w,h,cb;
  183.     BYTE  *buf;
  184.     IMAGE *d_image;
  185.  
  186.     w = s_image->vres;
  187.     h = s_image->hres;
  188.  
  189.     // Create new image buffer
  190.     d_image = CreateImage(w,h);
  191.  
  192.     // Copy image parameters
  193.     d_image->color = s_image->color;
  194.     strcpy(d_image->fspec,s_image->fspec);
  195.  
  196.     // Transform the data to the new image
  197.     buf = (BYTE *)malloc(s_image->hres);            // Allocate scan buffer.
  198.     for (row=0; row<s_image->vres; ++row)           // For each row in the image
  199.         {                                          // Write the row to the col
  200.         x  = 0;
  201.         y  = row;
  202.         cb = s_image->hres;
  203.         GetImageRow(s_image,x,y,buf,cb);               // Read pixel row from image
  204.  
  205.         if (dir == -1)
  206.             { x  = row; y  = s_image->hres-1; cb = -s_image->hres; }
  207.         if (dir == 1)
  208.             { x  = s_image->vres-1-row; y = 0; cb = s_image->hres; }
  209.  
  210.         PutImageCol(d_image,x,y,buf,cb);               // Transpose to column
  211.         }
  212.     free(buf);                                    // Free the scan buffer.
  213.  
  214.     // Copy palette data to the new image
  215.     CopyImagePal(s_image,d_image);
  216.  
  217.     // Return the new image.
  218.     return(d_image);
  219.     }
  220.  
  221. /*
  222. --------------------------------------------------------------------------
  223. int  ReduceImage(IMAGE *image,BYTE huge *hpBuf,int reduction)
  224.  
  225. DESCRIPTION:
  226. Copy a complete image from source buffer to destination buffer. At
  227. the same time an image reduction is performed to shrink the image.
  228. Only integer reductions are performed.
  229. --------------------------------------------------------------------------
  230. */
  231. IMAGE *ReduceImage(IMAGE *s_image,int reduction)
  232.     {
  233.     int  d,w,h,x,y;
  234.     BYTE huge *hpDataIn;
  235.     BYTE huge *hpDataOut;
  236.     IMAGE  *d_image;
  237.  
  238.     if (!s_image->hData)                               /* Check Frame Buffer   */
  239.       return(NULL);
  240.    if (reduction<0 || reduction>4)               /* Check bounds         */
  241.         return(NULL);
  242.  
  243.     d = pow(2,reduction);
  244.     w = s_image->hres / d;
  245.     h = s_image->vres / d;
  246.  
  247.     // Create new image buffer
  248.     d_image = CreateImage(w,h);
  249.  
  250.     // Copy image parameters
  251.     d_image->color = s_image->color;
  252.     strcpy(d_image->fspec,s_image->fspec);
  253.  
  254.     hpDataOut = (BYTE huge *)GlobalLock(d_image->hData);         /* Lock the IMAGE memory   */
  255.     hpDataIn = (BYTE huge *)GlobalLock(s_image->hData);   /* Lock the IMAGE memory   */
  256.  
  257.     for (y=0; y<d_image->vres; ++y)
  258.       for (x=0; x<d_image->hres; ++x)
  259.             *(hpDataOut+ (DWORD)(d_image->vres-1-y  )* d_image->scansize+x  ) =
  260.             *(hpDataIn + (DWORD)(s_image->vres-1-y*d)*s_image->scansize+x*d);
  261.  
  262.     GlobalUnlock(s_image->hData);                       // Unlock the IMAGE memory
  263.     GlobalUnlock(d_image->hData);                        // Unlock the IMAGE memory
  264.